using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using pal.SvnConf.Core;

namespace pal.SvnConf.GUI
{
    //Understands adapting from the raw list of users to a conveniently renderable format
    public class UsersDataSetView : DataSet, IBindingSource
    {
        public const string USERS_DATATABLE_NAME = "Users";
        public const string USERNAME_COLUMN = "Username";
        public const string PASSWORD_COLUMN = "Password";
        private readonly List<User> _users;
        private DataTable _usersTable;

        public UsersDataSetView(List<User> users)
        {
            _users = users;
            initializeTable();
            setData();
        }

        public BindingSource BindingSource
        {
            get { return new BindingSource(this, USERS_DATATABLE_NAME); }
        }

        private void setData()
        {
            foreach (User user in _users)
                _usersTable.Rows.Add(getRowDataFor(user));
        }

        private static object[] getRowDataFor(User user)
        {
            return new object[] {user.Name, user.Password};
        }

        private void initializeTable()
        {
            _usersTable = new DataTable(USERS_DATATABLE_NAME);
            setSchema();
            Tables.Add(_usersTable);
        }

        private void setSchema()
        {
            _usersTable.Columns.Add(USERNAME_COLUMN);
            _usersTable.Columns.Add(PASSWORD_COLUMN);
        }
    }
}